// pages/activity/create_personal.js
import { config } from "../../config/config";
import { Activity } from "../../model/activity";
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hidden: false,
    fileList: [],
    title: '',
    start_time: '',
    end_time: '',
    num_status: '0',
    number: '',
    man_num: '',
    women_num: '',
    content: '',
    content_length: 0,
    zy_content: '',
    zy_content_length: 0,
    minDate: new Date().getTime(),
    maxDate: new Date(2030, 1, 1).getTime(),
    currentDate: new Date().getTime(),
    start_show: false,
    end_show: false,
    sub_loading: false,
  },
  /**
   * 创建个人活动
   */
  async createData() {
    this.setData({
      sub_loading: true,
    })
    const res = await Activity.createGrActivity({
      fileList: this.data.fileList,
      title: this.data.title,
      start_time: this.data.start_time,
      end_time: this.data.end_time,
      num_status: this.data.num_status,
      number: this.data.number,
      man_num: this.data.man_num,
      women_num: this.data.women_num,
      content: this.data.content,
      zy_content: this.data.zy_content,
    },"POST");
    if(res.error_code != 10000){
      this.setData({
        sub_loading: false,
      });
      wx.showModal({
        title: '温馨提示',
        content: res.msg,
        showCancel: false,
        confirmText: '知道了',
        confirmColor: '#F3474E',
        success:function(res){
          if (res.confirm){
            wx.switchTab({
              url: './activity',
            })
          }
        }
      })
    } else {
      wx.showToast({
        title: res.msg,
        icon: 'none',
        duration: 2000
      })
      this.setData({
        sub_loading: false,
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad (options) {
    this.setData({
      hidden: true,
    })
    if(options.id) {
      const res = await Activity.getGrActivityInfo(options.id);
      console.log(res);
      const fileList = this.data.fileList;
      if(res.imgs) {
        res.imgs.forEach((item)=>{
          fileList.push({ url: item.img.url, img_id: item.img.id });
        })
      }
      const title = res.title,
      start_time = res.start_time,
      end_time = res.end_time,
      num_status =  res.num_status,
      number = res.number,
      man_num = res.man_num,
      women_num = res.women_num,
      content = res.content,
      zy_content = res.zy_content;
      this.setData({
        fileList,
        title,
        start_time,
        end_time,
        num_status,
        number,
        man_num,
        women_num,
        content,
        content_length: content.length,
        zy_content_length: zy_content.length,
        zy_content,
      })
    }
  },
  afterRead(event) {
    const { file } = event.detail;
    const self = this
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    wx.uploadFile({
      url: config.apiFileUrl, // 仅为示例，非真实的接口地址
      filePath: file.url,
      name: 'file',
      success(res) {
        const fileData = JSON.parse(res.data)
        // 上传完成需要更新 fileList
        const fileList = self.data.fileList;
        fileList.push({ ...file, url: fileData[0].url, img_id: fileData[0].id });
        self.setData({ fileList });
      },
    });
  },
  // 标题
  changeTitle(e) {
    this.setData({
      title: e.detail.value,
    })
  },
  // 切换人数限制
  onClickNum(e) {
    if(e.currentTarget.dataset.status == this.data.num_status) {
        return false;
    }
    this.setData({
      num_status: e.currentTarget.dataset.status
    })
  },
  // 输入人数
  changeNumber(e) {
    this.setData({
      number: e.detail.value,
    })
  },
  //输入男生人数
  changeManNum(e) {
    this.setData({
      man_num: e.detail.value,
    })
  },
  // 输入男生人数
  changeWoManNum(e) {
    this.setData({
      woman_num: e.detail.value,
    })
  },
  // 输入活动简介
  changeContent(e) {
    if(e.detail.value.length > 255) {
      return false;
    }
    this.setData({
      content: e.detail.value,
      content_length: e.detail.value.length,
    })
  },
  // 输入注意事项
  changeZyContent(e) {
    if(e.detail.value.length > 255) {
      return false;
    }
    this.setData({
      zy_content: e.detail.value,
      zy_content_length: e.detail.value.length,
    })
  },
  onChangeStart(event) {
    const time = event.detail;
    this.setData({
      start_time: app.getDateTime(time),
      start_show: false,
    });
  },
  onClickStartShow() {
    this.setData({
      start_show: true,
    })
  },
  onClickStartHide() {
    this.setData({
      start_show: false,
    })
  },
  onChangeEnd(event) {
    const time = event.detail;
    this.setData({
      end_time: app.getDateTime(time),
      end_show: false,
    });
  },
  onClickEndShow() {
    this.setData({
      end_show: true,
    })
  },
  onClickEndHide() {
    this.setData({
      end_show: false,
    })
  },
  delImg(e) {
    const index = e.detail.index;
    let fileList = this.data.fileList;
    fileList.splice(index, 1);
    this.setData({
      fileList: fileList,
    })
  }
})